<template>
  <div>
    <!--搜索表单-->
    <div style="margin: 10px 0">
      <el-input prefix-icon="el-icon-user" style="width: 240px;margin-right: 5px" placeholder="请输入图书名称"
                v-model="params.bookName"></el-input>
      <el-input prefix-icon="el-icon-user" style="width: 240px;margin-right: 5px" placeholder="请输入图书标准码"
                v-model="params.bookNo"></el-input>
      <el-input prefix-icon="el-icon-user" style="width: 240px;" placeholder="请输入用户名称"
                v-model="params.userName"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>
    </div>

    <!--批量删除-->
    <el-popconfirm
        confirm-button-text='确定'
        cancel-button-text='我再想想'
        icon="el-icon-info"
        icon-color="red"
        title="您确定批量删除这些数据吗？"
        @confirm="delBatch"
    >
      <el-button type="danger" slot="reference" style="margin-bottom:10px">批量删除 <i class="el-icon-remove-outline"></i>
      </el-button>
    </el-popconfirm>

    <!--数据表格-->
    <el-table :data="tableDate" stripe row-key="id" default-expand-all  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center"></el-table-column>
      <el-table-column prop="id" label="编号" width="50px"></el-table-column>
      <el-table-column prop="bookName" label="图书名称" width="80px"></el-table-column>
      <el-table-column prop="bookNo" label="图书标准码"></el-table-column>
      <el-table-column prop="userNo" label="会员码"></el-table-column>
      <el-table-column prop="userName" label="用户名" width="80px"></el-table-column>
      <el-table-column prop="userPhone" label="用户联系方式"></el-table-column>
      <el-table-column prop="score" label="所用积分" width="80px"></el-table-column>
      <el-table-column prop="status" label="借出状态" width="80px"></el-table-column>
      <el-table-column prop="days" label="借出天数" width="80px"></el-table-column>
      <el-table-column prop="createTime" label="借出时间" width="100px"></el-table-column>
      <el-table-column prop="returnTime" label="归还时间" width="100px"></el-table-column>
      <el-table-column prop="realTime" label="实际归还时间" width="100px"></el-table-column>

      <el-table-column label="操作">
        <template v-slot="scope">
          <!--scope.row就是当前行的数据-->
          <template>
            <el-popconfirm title="您确定删除这行数据吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div style="margin-top: 10px">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          :page-sizes="[5, 10, 15, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: 'ReturnBook',
  data() {
    return {
      tableDate: [],       //1.存放数据
      total: 0,           //总条数
      multipleSelection: [],
      params: {            //分页模糊查询参数
        pageSize: 10,
        pageNum: 1,
        bookName: '',
        bookNo: '',
        userName: '',
      },
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {}
    }
  },
  created() {					//3.前端页面加载数据
    this.load()
  },
  methods: {
    load() {                 //2.请求后台数据
      request.get('/borrow/pageReturn', {
        params: this.params
      }).then(res => {
        console.log(res.data)
        if (res.code === '200') {
          this.tableDate = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    reset() {               //重置搜索框数据
      this.params = {
        pageSize: 10,
        pageNum: 1,
        bookName: '',
        bookNo: '',
        userName: '',
      }
      this.load()
    },
    handleCurrentChange(pageNum) {      //点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.load()
    },
    del(id) {
      request.delete("/borrow/deleteReturn/" + id).then(res => {
        if (res.code === '200') {
          this.$notify.success("删除成功")
          this.load()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    handleSelectionChange(val) {//多选
      // console.log(val)
      this.multipleSelection = val;
    },
    delBatch() { //批量删除
      let ids = this.multipleSelection.map(v => v.id)  //把对象数组变成纯id数组
      request.post('/borrow/deleteReturnBatch', ids).then(res => {
        if (res.code === '200') {
          this.$notify.success('批量删除成功')
          this.load()
        } else {
          this.$notify.error('批量删除失败')
        }
      })
    },
  }
}
</script>